<?php

namespace Tyrone\Lib;

class InternalHandler
{

    public function Receive($server, $fd, $reactor_id, $data,$conf){
        if(in_array($data,['shutdown','reload'])){
            if ($data === 'shutdown') {
                $server->shutdown();
                return;
            }

            if ($data === 'reload') {
                $server->reload();
                $server->send($fd, "Reload signal sent.\n");
                return;
            }
            $server->send($fd,$data);
            return;
        }else{
            if($data){
                $dataArr = json_decode($data,true);
                $command = $dataArr['command'] ?? '';
                $logCenter = $dataArr['logCenter'] ?? false;
                list($class,$method) = explode('::',$command);
                require_once DIR.'/vendor/autoload.php';
                $BaseWorker = new \Tyrone\Lib\SingleWorker($dataArr['sDate']??null,$dataArr['eDate']??null);
                $BaseWorker
                    ->setCrontabList([compact('class','method')])
                    ->setSid($dataArr['platform'],$dataArr['plat_server'])->setCenter($logCenter);
                $result = $BaseWorker->init($server,-1,$conf);
                $server->send($fd,$result ?:'result empty');
            }
        }
    }
}